package com.feiyu.shopsAndgoods.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feiyu.common.utils.PageUtils;
import com.feiyu.common.utils.Query;

import com.feiyu.shopsAndgoods.dao.GoodsDao;
import com.feiyu.shopsAndgoods.entity.GoodsEntity;
import com.feiyu.shopsAndgoods.service.GoodsService;


@Service("goodsService")
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, GoodsEntity> implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<GoodsEntity> page = this.page(
                new Query<GoodsEntity>().getPage(params),
                new QueryWrapper<GoodsEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByGoodTagName(Map<String, Object> params) {
        QueryWrapper<GoodsEntity> wrapper = new QueryWrapper<>();
        List<Integer> goodsTagId = (List<Integer>) params.get("goodsTagId");
        for (int i = 0; i < goodsTagId.size(); i++) {
            if (i == (goodsTagId.size() - 1)) {
                wrapper.eq("goods_tag_id", goodsTagId.get(i));
            } else {
                wrapper.eq("goods_tag_id", goodsTagId.get(i)).or();
            }
        }
        IPage<GoodsEntity> page = this.page(
                new Query<GoodsEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByTagId(Map<String, Object> params) {
        QueryWrapper<GoodsEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_tag_id", params.get("goodsTagId"));
        IPage<GoodsEntity> page = this.page(
                new Query<GoodsEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByGoodsName(Map<String, Object> params) {
        QueryWrapper<GoodsEntity> wrapper = new QueryWrapper<>();
        wrapper.like("goods_name", params.get("goodsName"));
        IPage<GoodsEntity> page = this.page(
                new Query<GoodsEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByShopId(Map<String, Object> params) {
        QueryWrapper<GoodsEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("shop_id", params.get("shopId"));
        IPage<GoodsEntity> page = this.page(
                new Query<GoodsEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<GoodsEntity> getAllByShopId(Integer shopId) {
        return goodsDao.getAllByShopId(shopId);
    }

    @Override
    public List<GoodsEntity> getRecommendGoodsByNum(Integer recommendGoodsNumber) {
        return goodsDao.getRecommendGoodsByNum(recommendGoodsNumber);
    }

}